package com.oneday.ashop.console.authority;

import com.oneday.ashop.core.base.mongo.BaseMongoDaoSupport;
import com.oneday.ashop.core.entity.Admin;
import com.oneday.ashop.core.entity.Page;
import com.oneday.ashop.core.entity.Resource;
import com.oneday.ashop.core.entity.Role;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 权限管理持久层
 * Created by admin on 2016/7/3.
 */
@Repository
public class AuthDao extends BaseMongoDaoSupport{
    /**
     * 添加资源
     * @param resource
     */
    public void addResource(Resource resource) {
        insert(resource);
    }

    /**
     * 查找更新资源
     * @param resourceId
     * @param update
     */
    public void findAndModifyResource(Integer resourceId, Update update) {
        Query query = Query.query(Criteria.where("_id").is(resourceId));
        findAndModify(query, update, Resource.class);
    }

    /**
     * 查找资源
     * @return
     */
    public List<Resource> findAllResources() {
        Query query = new Query(Criteria.where("status").ne(Resource.STATUS_DELETE).and("type").is(Resource.TYPE_PAGE));
        return find(query, Resource.class);
    }

    /**
     * 查找所有的角色
     * @return
     */
    public List<Role> findALlRoles() {
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC, "_id"));
        return find(query, Role.class);
    }

    /**
     * 添加角色
     * @param role
     */
    public void addRole(Role role) {
        insert(role);
    }

    /**
     * 修改角色的资源
     * @param roleId
     * @param update
     */
    public void updateRole(Integer roleId, Update update) {
        Query query = Query.query(Criteria.where("_id").is(roleId));
        findAndModify(query, update, Role.class);
    }

    /**
     * 查找所有的资源
     * @return
     */
    public List<Role> findRoles() {
        Query query = new Query();
        query.with(new Sort(Sort.Direction.ASC, "_id"));
        return find(query, Role.class);
    }

    /**
     * 查询管理员的分页数据
     * @param page
     */
    public void findAdminPage(Page<Admin> page) {
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC, "_id"));
        findEntityPage(Admin.class, page, query);
    }

    /**
     * 添加管理员
     * @param admin
     */
    public void addAdmin(Admin admin) {
        insert(admin);
    }

    /**
     * 更新管理员
     * @param adminId
     * @param update
     */
    public void updateAdmin(Integer adminId, Update update) {
        Query query = Query.query(Criteria.where("_id").is(adminId));
        findAndModify(query, update, Admin.class);
    }

    /**
     * 查找角色
     * @param roleIds
     */
    public List<Role> findRoles(List<Integer> roleIds) {
        Query query = Query.query(Criteria.where("_id").in(roleIds));
        return find(query, Role.class);
    }

    /**
     * 根据名称查找管理员
     * @param name
     * @return
     */
    public Admin findAdmin(String name) {
        Query query = Query.query(Criteria.where("name").is(name));
        return findOne(query, Admin.class);
    }

    /**
     * 根据管理员id查询管理员信息
     * @param id
     * @return
     */
    public Admin findAdminById(Integer id) {
        Query query = Query.query(Criteria.where("_id").is(id));
        return findOne(query, Admin.class);
    }

    /**
     * 根据id查找角色
     * @param id
     * @return
     */
    public Role findRoleById(Integer id, String...fields) {
        Query query = Query.query(Criteria.where("_id").is(id));
        includes(query, fields);
        return findOne(query, Role.class);
    }

}
